export * from "../../../../mac-xiang/method/src";
export * from "./static";
import * as md from "../../../../mac-xiang/method/src";
import { api } from "./api";
import { apiUrl } from "./static";

const ra = {
  group: 0,
  goodsType: 0,
};
function per(this: { permission: Uint8Array; }, k: keyof typeof pd) {
  const t = pd[k];
  if (t) {
    return !!(this.permission[t.i] & t.v);
  }
  return false;
}
export async function getSetting(id: string) {
  return ((await gd.setting.getById(id)) || { value: "" }).value;
}
export function fUrl(p: string) {
  return md.formatUrl(p, apiUrl);
}
class Format {
  readonly def = {
    get addr(): gt.sql.addr {
      return {
        id: "",
        createTime: 0,
        create: "",
        def: 0,
        name: "",
        addr: "",
        phone: "",
      };
    },
    get upload(): gt.sql.upload {
      return {
        /** 主键ID CHAR(20) NOT NULL  */
        id: "",
        /** 创建时间 INT(10) UNSIGNED NOT NULL DEFAULT 0  */
        createTime: 0,
        /** 创建者 CHAR(20) NOT NULL  */
        user: "",
        /** 类型 CHAR(255) NOT NULL  */
        type: "",
        /** 名称 CHAR(255) NOT NULL  */
        name: "",
        /** 地址 CHAR(255) NOT NULL  */
        path: "",
        /** 其他内容 TEXT NOT NULL  */
        other: {},
      };
    },
    get stock(): gt.sql.stock {
      return {
        /** 主键ID CHAR(20) NOT NULL  */
        id: "",
        /** 创建时间 INT(10) UNSIGNED NOT NULL DEFAULT 0  */
        createTime: 0,
        /** 归属商家 CHAR(20) NOT NULL  */
        gid: "",
        /** 商品id CHAR(20) NOT NULL  */
        goods: "",
        /** 数量 INT NOT NULL  */
        sum: 0,
      } as any;
    },
    get stockLog(): gt.sql.stockLog {
      return {
        /** 主键ID CHAR(20) NOT NULL  */
        id: "",
        /** 创建时间 INT(10) UNSIGNED NOT NULL  */
        createTime: 0,
        /** 创建用户 CHAR(20) NOT NULL  */
        user: "",
        /** 库存id CHAR(20) NOT NULL  */
        stock: "",
        /** 数量 INT NOT NULL  */
        sum: 0,
        /** 说明 TEXT NOT NULL  */
        des: "",
        _stock: this.stock
      };
    },
    get goodsType(): gt.sql.goodsType {
      return {
        /** 主键ID CHAR(20) NOT NULL  */
        id: "",
        /** 创建时间 INT(10) UNSIGNED NOT NULL  */
        createTime: "",
        /** 上级id CHAR(20) DEFAULT ''  */
        pid: "",
        /** 创建用户 CHAR(20) NOT NULL  */
        user: "",
        /** 名称 CHAR(255) NOT NULL  */
        name: "",
        /** 其他 TEXT NOT NULL  */
        other: {},
      };
    },
    get goods(): gt.sql.goods {
      return {
        /** 主键ID CHAR(20) NOT NULL  */
        id: "",
        /** 创建时间 INT(10) UNSIGNED NOT NULL  */
        createTime: "",
        /** 创建用户 CHAR(20) NOT NULL  */
        user: "",
        /** 名称 CHAR(255) NOT NULL  */
        name: "",
        /** 类型 CHAR(255) NOT NULL  */
        type: "",
        /** 价格(单位:分) INT NOT NULL  */
        money: 0,
        /** 横幅 TEXT NOT NULL  */
        banner: [],
        /** 说明 TEXT NOT NULL  */
        des: "",
        tagOne: "",
        tagTow: "",
        sold: 0,
      };
    },
    get order(): gt.sql.order {
      return {
        /** 主键ID CHAR(32) NOT NULL COMMENT  */
        id: "",
        /** 商户id CHAR(32) NOT NULL DEFAULT '' COMMENT  */
        gid: "",
        /** 创建时间 INT(10) UNSIGNED NOT NULL DEFAULT 0 COMMENT  */
        createTime: 0,
        /** 支付时间 INT(10) UNSIGNED NOT NULL DEFAULT 0 COMMENT  */
        payTime: 0,
        /** 发货时间 INT(10) UNSIGNED NOT NULL DEFAULT 0 COMMENT  */
        sendTime: 0,
        /** 收货时间 INT(10) UNSIGNED NOT NULL DEFAULT 0 COMMENT  */
        recvTime: 0,
        /** 收货地址 TEXT NOT NULL COMMENT  */
        recvAddr: { name: "", phone: "", addr: "" },
        /** 支付id CHAR(64) NOT NULL COMMENT  */
        oid: "",
        /** 创建用户 CHAR(32) NOT NULL COMMENT  */
        create: "",
        /** 名称 CHAR(255) NOT NULL COMMENT  */
        name: "",
        /** 价格(单位:分) INT NOT NULL COMMENT  */
        money: 0,
        /** 说明 TEXT NOT NULL COMMENT  */
        des: "",
        /** 奖品id CHAR(32) NOT NULL COMMENT  */
        prize: "",
        /** 奖品核销商户 CHAR(32) NOT NULL COMMENT  */
        prizeGroup: "",


        /** 详细内容 TEXT NOT NULL  */
        content: [],
      };
    },
    get msg(): gt.sql.msg {
      return {
        /** 主键ID CHAR(20) NOT NULL  */
        id: "",
        /** 创建时间 INT(10) UNSIGNED NOT NULL  */
        createTime: 0,
        /** 阅读时间 INT(10) UNSIGNED NOT NULL DEFAULT 0  */
        readTime: "",
        /** 创建者 CHAR(20) NOT NULL  */
        create: "",
        /** 创建者删除 INT(10) UNSIGNED NOT NULL DEFAULT 0  */
        cd: 0,
        /** 接收者 CHAR(20) NOT NULL  */
        target: "",
        /** 接收者删除 INT(10) UNSIGNED NOT NULL DEFAULT 0  */
        td: 0,
        /** 消息类型 CHAR(255) DEFAULT NULL  */
        type: "",
        /** 内容 TEXT NOT NULL  */
        data: "",
      };
    },
    get group(): gt.sql.group {
      return {
        /** 主键ID CHAR(20) NOT NULL  */
        id: "",
        /** 创建时间 INT(10) UNSIGNED NOT NULL  */
        createTime: 0,
        /** 上级id CHAR(20) DEFAULT ''  */
        pid: "",
        create: "",
        /** 状态 INT(11) DEFAULT 0  */
        state: 0,
        /** 拥有权限 BINARY(20) DEFAULT NULL  */
        permission: new Uint8Array(20),
        /** 名称 CHAR(255) NOT NULL  */
        name: "",
        /** 设置 LONGTEXT NOT NULL  */
        other: {},
        per,
      };
    },
    get user() {
      return {
        /** 主键ID CHAR(20) NOT NULL  */
        id: "",
        /** 创建时间 INT(10) UNSIGNED NOT NULL  */
        createTime: 0,
        /** 分类 CHAR(255) DEFAULT '消费者'  */
        gid: "",
        /** 上级id CHAR(20) DEFAULT ''  */
        pid: "",
        /** wxid CHAR(20) NOT NULL DEFAULT ''  */
        wxid: "",
        /** 状态 INT(11) DEFAULT 0  */
        state: 0,
        /** 拥有权限 BINARY(20) DEFAULT NULL  */
        permission: new Uint8Array(20),
        /** 用户名 CHAR(190) NOT NULL  */
        username: "",
        /** 密码 CHAR(255) NOT NULL  */
        password: "",
        /** 名称 CHAR(255) NOT NULL  */
        name: "",
        /** 提成百分比 TINYINT UNSIGNED NOT NULL DEFAULT 0  */
        commission: 0,
        /** 令牌 CHAR(36) DEFAULT NULL  */
        token: "",
        /** 设置 LONGTEXT NOT NULL  */
        other: {},
        per,
      };
    },
    get setting(): gt.sql.setting {
      return {
        /** 主键ID CHAR(20) NOT NULL  */
        id: "",
        /** 名称 CHAR(255) NOT NULL  */
        name: "",
        /** 内容 TEXT NOT NULL  */
        value: "",
      };
    },
    get prize(): gt.sql.prize {
      return {
        /** 主键ID CHAR(190)  NOT NULL  */
        id: "",
        /** 名称 CHAR(255)  NOT NULL  */
        name: "",
        /** 名称颜色 CHAR(9)  NOT NULL  */
        nameColor: "",
        /** 背景颜色 CHAR(9)  NOT NULL  */
        bgColor: "",
        /** 图片地址 TEXT   */
        image: "",
        /** 数量 INT   */
        sum: 0,
        /** 概率 INT   */
        weight: 0,
        /** 内容 CHAR(255)  NOT NULL  */
        value: "",
      };
    },
    get prizeGroup(): gt.sql.prizeGroup {
      return {
        /** 主键ID CHAR(190)  NOT NULL  */
        id: "",
        create: "", user: "", group: "", cashTime: 0,
        /** 创建时间 INT(10)  UNSIGNED NOT NULL  */
        createTime: 0,
        /** 首次扫码时间 INT(10)  UNSIGNED NOT NULL DEFAULT 0  */
        useTime: 0,
        /** 名称 CHAR(255)  NOT NULL  */
        name: "",
        /** 奖品id CHAR(190)   */
        prize: "",
        /** 奖池内容 TEXT   */
        content: [],
        _content: [],
        num: 0,
      };
    },
  } as const;
  constructor () { }
  url(p: string) {
    return fUrl(p);
  }
  private isPages(p: any): p is gt.PageData<any> {
    if (p && md.isNumber(p.current) && md.isNumber(p.total) && md.isNumber(p.size)) {
      if (md.isArr(p.data)) {
        return true;
      } else if (md.isArr(p.records)) {
        p.data = p.records;
        delete p.records;
        return true;
      }
    }
    return false;
  }
  setDef<T extends obj>(dist: obj, def: T) {
    // @ts-ignore
    Object.keys(def).forEach(k => { dist[k] = dist[k] || def[k]; });
    return dist as T;
  }
  async setEntity<K extends keyof Omit<typeof gd, "groupTree" | "goodsTypeTree">>(p: obj, k: K, id: string, prop?: string) {
    if (!prop) { prop = `_${k}`; };
    p[prop] = await gd[k].getById(id);
    if (!p[prop]) {
      p[prop] = this.def[k];
    }
  }
  setTime(p: obj, key?: string) {
    if (key) {
      p[key] = parseInt(p[key]);
      p[`_${key}`] = md.formatDate(p[key]);
    } else {
      Object.keys(p).forEach(k => {
        if (/time/ig.test(k) && md.isNumber(p[k])) {
          p[k] = parseInt(p[k]);
          p[`_${k}`] = md.formatDate(p[k]);
        }
      });
    }
  }
  async addr(p?: any): Promise<any> {
    if (Array.isArray(p)) {
      await Promise.all(p.map(async (v) => { await this.addr(v); }));
      return p;
    } else if (this.isPages(p)) {
      await this.addr(p.data);
      return p;
    } else if (p) {
      this.setDef(p, this.def.addr);

      return p;
    }
  };
  async upload(p?: any): Promise<any> {
    if (Array.isArray(p)) {
      await Promise.all(p.map(async (v) => { await this.upload(v); }));
      return p;
    } else if (this.isPages(p)) {
      await this.upload(p.data);
      return p;
    } else if (p) {
      this.setDef(p, this.def.upload);
      if (typeof p.permission == "string") {
        p.permission = md.s2u8a(p.permission);
      }
      p.other = md.strParse(p.other, {});

      return p;
    }
  };
  async stock(p?: any): Promise<any> {
    if (Array.isArray(p)) {
      await Promise.all(p.map(async (v) => { await this.stock(v); }));
      return p;
    } else if (this.isPages(p)) {
      await this.stock(p.data);
      return p;
    } else if (p) {
      this.setDef(p, this.def.stock);
      if (typeof p.permission == "string") {
        p.permission = md.s2u8a(p.permission);
      }
      await this.setEntity(p, "group", p.gid);
      await this.setEntity(p, "goods", p.goods);
      this.setTime(p);
      return p;
    }
  };
  async stockLog(p?: any): Promise<any> {
    if (Array.isArray(p)) {
      await Promise.all(p.map(async (v) => { await this.stockLog(v); }));
      return p;
    } else if (this.isPages(p)) {
      await this.stockLog(p.data);
      return p;
    } else if (p) {
      this.setDef(p, this.def.stockLog);
      if (typeof p.permission == "string") {
        p.permission = md.s2u8a(p.permission);
      }
      p.other = md.strParse(p.other, {});
      this.setTime(p);
      await this.setEntity(p, "user", p.user);
      await this.setEntity(p, "stock", p.stock);
      return p;
    }
  };
  async goodsType(p?: any): Promise<any> {
    if (Array.isArray(p)) {
      await Promise.all(p.map(async (v) => { await this.goodsType(v); }));
      return p;
    } else if (this.isPages(p)) {
      await this.goodsType(p.data);
      return p;
    } else if (p) {
      this.setDef(p, this.def.goodsType);
      this.setTime(p);
      this.setEntity(p, "user", p.user);
      this.setEntity(p, "goodsType", p.pid);

      p.other = md.strParse(p.other, {});
      return p;
    }
  };
  async goods(p?: any): Promise<any> {
    if (Array.isArray(p)) {
      await Promise.all(p.map(async (v) => { await this.goods(v); }));
      return p;
    } else if (this.isPages(p)) {
      await this.goods(p.data);
      return p;
    } else if (p) {
      this.setDef(p, this.def.goods);
      p.other = md.strParse(p.other, {});
      p._createTime = md.formatDate(p.createTime);
      this.setEntity(p, "goodsType", p.type);
      p.banner = md.strParse(p.banner, []);
      p.banner.forEach((e: any) => {
        e.url = fUrl(e.url);
      });
      return p;
    }
  };
  async order(p?: any): Promise<any> {
    if (Array.isArray(p)) {
      await Promise.all(p.map(async (v) => { await this.order(v); }));
      return p;
    } else if (this.isPages(p)) {
      await this.order(p.data);
      return p;
    } else if (p) {
      this.setDef(p, this.def.order);
      if (typeof p.permission == "string") {
        p.permission = md.s2u8a(p.permission);
      }
      this.setTime(p);
      const c: any = md.strParse(p.content, []);
      if (typeof c[0] == "string") {
        p.content = await gd.stockLog.read({ id: c });
      } else {
        p.content = await format.stockLog(c);
      }
      p.recvAddr = md.strParse(p.recvAddr, {});
      p._recvAddr = p.recvAddr.phone ? `${p.recvAddr.name || ""}:${p.recvAddr.phone || ""} ${p.recvAddr.addr || ""}` : "联系方式为空";
      if (p.recvTime > 0) {
        p._t = "已签收";
      } else if (p.sendTime > 0) {
        p._t = "已发货";
      } else if (p.payTime > 0) {
        p._t = "已付款";
      } else {
        p._t = "待付款";
      }
      return p;
    }
  };
  async msg(p?: any): Promise<any> {
    // uni.hideLoading();
    if (Array.isArray(p)) {
      await Promise.all(p.map(async (v) => { await this.msg(v); }));
      return p;
    } else if (this.isPages(p)) {
      await this.msg(p.data);
      return p;
    } else if (p) {
      this.setDef(p, this.def.msg);
      if (typeof p.permission == "string") {
        p.permission = md.s2u8a(p.permission);
      }
      p.other = md.strParse(p.other, {});
      this.setTime(p);
      this.setTime(p, "readTime");
      this.setEntity(p, "user", p.create, "_create");
      this.setEntity(p, "user", p.target, "_target");
      return p;
    }
  };
  async group(p?: any): Promise<any> {
    if (Array.isArray(p)) {
      await Promise.all(p.map(async (v) => { await this.group(v); }));
      return p;
    } else if (this.isPages(p)) {
      await this.group(p.data);
      return p;
    } else if (p) {
      this.setDef(p, this.def.group);
      if (typeof p.permission == "string") {
        p.permission = md.s2u8a(p.permission);
      }
      this.setTime(p);
      p.other = md.strParse(p.other, {});
      this.setEntity(p, "user", p.create);
      return p;
    }
  };
  async user(p?: any): Promise<any> {
    if (Array.isArray(p)) {
      await Promise.all(p.map(async (v) => { await this.user(v); }));
      return p;
    } else if (this.isPages(p)) {
      await this.user(p.data);
      return p;
    } else if (p) {
      this.setDef(p, this.def.user);
      if (typeof p.permission == "string") {
        p.permission = md.s2u8a(p.permission);
      }
      this.setTime(p);
      this.setEntity(p, "group", p.gid, "_group");
      p._state = p.state == 0 ? "正常" : "禁用";
      this.setEntity(p, "user", p.pid, "_pid");
      p.other = md.strParse(p.other, {});
      p.money = parseInt(p.money) || 0;
      p.moneys = (p.money / 100).toFixed(2);
      return p;
    }
  };
  async setting(p?: any): Promise<any> {
    if (Array.isArray(p)) {
      await Promise.all(p.map(async (v) => { await this.setting(v); }));
      return p;
    } else if (this.isPages(p)) {
      await this.setting(p.data);
      return p;
    } else if (p) {
      this.setDef(p, this.def.setting);
      if (typeof p.permission == "string") {
        p.permission = md.s2u8a(p.permission);
      }
      p.other = md.strParse(p.other, {});

      return p;
    }
  };
  async prize(p?: any): Promise<any> {
    if (Array.isArray(p)) {
      await Promise.all(p.map(async (v) => { await this.prize(v); }));
      return p;
    } else if (this.isPages(p)) {
      await this.prize(p.data);
      return p;
    } else if (p) {
      this.setDef(p, this.def.prize);
      if (p.value) {
        if (!p.value.indexOf("m")) {
          p._v = `现金: ${(parseInt(p.value.substring(6)) / 100).toFixed(2)}元`;
        } else if (!p.value.indexOf("g")) {
          p._v = "商品: " + (await gd.goods.getById(p.value.substring(6)))?.name;
        }

      }
      return p;
    }
  };
  async prizeGroup(p?: any): Promise<any> {
    if (Array.isArray(p)) {
      await Promise.all(p.map(async (v) => { await this.prizeGroup(v); }));
      return p;
    } else if (this.isPages(p)) {
      await this.prizeGroup(p.data);
      return p;
    } else if (p) {
      this.setDef(p, this.def.prizeGroup);
      p._createTime = md.formatDate(p.createTime);
      p._content = await gd.prize.getById(p.content);
      return p;
    }
  };
}
export const
  format = new Format(),
  gd = {
    addr: new md.BaseArr<"addr", gt.sql.addr>({
      type: "addr",
      async rid(ids, type, _this) {
        if (!ids) { return; }
        return await format.addr(await api.request({ id: ids }, `${type}/read`));
      },
      async c(e, type, _this) {
        return await format.addr(await api.request(e, `${type}/create`) as gt.sql.addr);
      },
      async u(e, type, _this) {
        return await api.request(e, `${type}/update`) as number;
      },
      async r(e, type, _this) {
        return await format.addr(await api.request(e, `${type}/read`));
      },
      async d(e, type, _this) {
        return await api.request(e, `${type}/delete`) as number;
      },
    }),
    upload: new md.BaseArr<"upload", gt.sql.upload>({
      type: "upload",
      async rid(ids, type, _this) {
        if (!ids) { return; }
        return await format.upload(await api.request({ id: ids }, `${type}/read`));
      },
      async c(e, type, _this) {
        return await format.upload(await api.request(e, `${type}/create`) as gt.sql.upload);
      },
      async u(e, type, _this) {
        return await api.request(e, `${type}/update`) as number;
      },
      async r(e, type, _this) {
        return await format.upload(await api.request(e, `${type}/read`));
      },
      async d(e, type, _this) {
        return await api.request(e, `${type}/delete`) as number;
      },
    }),
    stock: new md.BaseArr<"stock", gt.sql.stock>({
      type: "stock",
      async rid(ids, type, _this) {
        if (!ids) { return; }
        return await format.stock(await api.request({ id: ids }, `${type}/read`));
      },
      async c(e, type, _this) {
        return await format.stock(await api.request(e, `${type}/create`) as gt.sql.stock);
      },
      async u(e, type, _this) {
        return await api.request(e, `${type}/update`) as number;
      },
      async r(e, type, _this) {
        return await format.stock(await api.request(e, `${type}/read`));
      },
      async d(e, type, _this) {
        return await api.request(e, `${type}/delete`) as number;
      },
    }),
    stockLog: new md.BaseArr<"stockLog", gt.sql.stockLog>({
      type: "stockLog",
      async rid(ids, type, _this) {
        if (!ids) { return; }
        return await format.stockLog(await api.request({ id: ids }, `${type}/read`));
      },
      async c(e, type, _this) {
        return await format.stockLog(await api.request(e, `${type}/create`) as gt.sql.stockLog);
      },
      async u(e, type, _this) {
        return await api.request(e, `${type}/update`) as number;
      },
      async r(e, type, _this) {
        return await format.stockLog(await api.request(e, `${type}/read`));
      },
      async d(e, type, _this) {
        return await api.request(e, `${type}/delete`) as number;
      },
    }),
    goodsType: new md.BaseArr<"goodsType", gt.sql.goodsType>({
      type: "goodsType",
      async rid(ids, type, _this) {
        if (!ids) { return; }
        return await format.goodsType(await api.request({ id: ids }, `${type}/read`));
      },
      async c(e, type, _this) {
        return await format.goodsType(await api.request(e, `${type}/create`) as gt.sql.goodsType);
      },
      async u(e, type, _this) {
        return await api.request(e, `${type}/update`) as number;
      },
      async r(e, type, _this) {
        if (!e || JSON.stringify(e) == "{}") ra.goodsType = 1;
        return await format.goodsType(await api.request(e, `${type}/read`));
      },
      async d(e, type, _this) {
        return await api.request(e, `${type}/delete`) as number;
      },
    }),
    goods: new md.BaseArr<"goods", gt.sql.goods>({
      type: "goods",
      async rid(ids, type, _this) {
        if (!ids) { return; }
        return await format.goods(await api.request({ id: ids }, `${type}/read`));
      },
      async c(e, type, _this) {
        return await format.goods(await api.request(e, `${type}/create`) as gt.sql.goods);
      },
      async u(e, type, _this) {
        return await api.request(e, `${type}/update`) as number;
      },
      async r(e, type, _this) {
        return await format.goods(await api.request(e, `${type}/read`));
      },
      async d(e, type, _this) {
        return await api.request(e, `${type}/delete`) as number;
      },
    }),
    order: new md.BaseArr<"order", gt.sql.order>({
      type: "order",
      async rid(ids, type, _this) {
        if (!ids) { return; }
        return await format.order(await api.request({ id: ids }, `${type}/read`));
      },
      async c(e, type, _this) {
        return await format.order(await api.request(e, `${type}/create`) as gt.sql.order);
      },
      async u(e, type, _this) {
        return await api.request(e, `${type}/update`) as number;
      },
      async r(e, type, _this) {
        return await format.order(await api.request(e, `${type}/read`));
      },
      async d(e, type, _this) {
        return await api.request(e, `${type}/delete`) as number;
      },
    }),
    msg: new md.BaseArr<"msg", gt.sql.msg>({
      type: "msg",
      async rid(ids, type, _this) {
        if (!ids) { return; }
        return await format.msg(await api.request({ id: ids }, `${type}/read`));
      },
      async c(e, type, _this) {
        return await format.msg(await api.request(e, `${type}/create`) as gt.sql.msg);
      },
      async u(e, type, _this) {
        return await api.request(e, `${type}/update`) as number;
      },
      async r(e, type, _this) {
        return await format.msg(await api.request(e, `${type}/read`));
      },
      async d(e, type, _this) {
        return await api.request(e, `${type}/delete`) as number;
      },
    }),
    group: new md.BaseArr<"group", gt.sql.group>({
      type: "group",
      async rid(ids, type, _this) {
        if (!ids) { return; }
        return await format.group(await api.request({ id: ids }, `${type}/read`));
      },
      async c(e, type, _this) {
        return await format.group(await api.request(e, `${type}/create`));
      },
      async u(e, type, _this) {
        return await api.request(e, `${type}/update`) as number;
      },
      async r(e, type, _this) {
        if (!e || JSON.stringify(e) == "{}") ra.group = 1;
        return await format.group(await api.request(e, `${type}/read`));
      },
      async d(e, type, _this) {
        return await api.request(e, `${type}/delete`) as number;
      },
    }),
    user: new md.BaseArr<"user", gt.sql.user>({
      type: "user",
      async rid(ids, type, _this) {
        if (!ids) { return; }
        return await format.user(await api.request({ id: ids }, `${type}/read`));
      },
      async c(e, type, _this) {
        return await format.user(await api.request(e, `${type}/create`) as gt.sql.user);
      },
      async u(e, type, _this) {
        return await api.request(e, `${type}/update`) as number;
      },
      async r(e, type, _this) {
        return await format.user(await api.request(e, `${type}/read`));
      },
      async d(e, type, _this) {
        return await api.request(e, `${type}/delete`) as number;
      },
    }),
    setting: new md.BaseArr<"setting", gt.sql.setting>({
      type: "setting",
      async rid(ids, type, _this) {
        if (!ids) { return; }
        return await format.setting(await api.request({ id: ids }, `${type}/read`));
      },
      async c(e, type, _this) {
        return await format.setting(await api.request(e, `${type}/create`) as gt.sql.setting);
      },
      async u(e, type, _this) {
        return await api.request(e, `${type}/update`) as number;
      },
      async r(e, type, _this) {
        return await format.setting(await api.request(e, `${type}/read`));
      },
      async d(e, type, _this) {
        return await api.request(e, `${type}/delete`) as number;
      },
    }),
    prize: new md.BaseArr<"prize", gt.sql.prize>({
      type: "prize",
      async rid(ids, type, _this) {
        if (!ids) { return; }
        return await format.prize(await api.request({ id: ids }, `${type}/read`));
      },
      async c(e, type, _this) {
        return await format.prize(await api.request(e, `${type}/create`) as gt.sql.prize);
      },
      async u(e, type, _this) {
        return await api.request(e, `${type}/update`) as number;
      },
      async r(e, type, _this) {
        return await format.prize(await api.request(e, `${type}/read`));
      },
      async d(e, type, _this) {
        return await api.request(e, `${type}/delete`) as number;
      },
    }),
    prizeGroup: new md.BaseArr<"prizeGroup", gt.sql.prizeGroup>({
      type: "prizeGroup",
      async rid(ids, type, _this) {
        if (!ids) { return; }
        return await format.prizeGroup(await api.request({ id: ids }, `${type}/read`));
      },
      async c(e, type, _this) {
        return await format.prizeGroup(await api.request(e, `${type}/create`) as gt.sql.prizeGroup);
      },
      async u(e, type, _this) {
        return await api.request(e, `${type}/update`) as number;
      },
      async r(e, type, _this) {
        return await format.prizeGroup(await api.request(e, `${type}/read`));
      },
      async d(e, type, _this) {
        return await api.request(e, `${type}/delete`) as number;
      },
    }),
    get groupTree() {
      return new Promise<Tree<gt.sql.group>>(async (resolve) => {
        resolve(md.arr2tree(ra.group ? gd.group : await gd.group.read(), "id", "pid", 1));
      });
    },
    get goodsTypeTree() {
      return new Promise<Tree<gt.sql.goodsType>>(async (resolve) => {
        resolve(md.arr2tree(ra.goodsType ? gd.goodsType : await gd.goodsType.read(), "id", "pid", 1));
      });
    }
  } as const,
  pd = {
    "上传文件创建": { i: 1, v: 1, d: "上传文件创建" },
    "上传文件修改": { i: 1, v: 2, d: "强制修改其他上传文件信息" },
    "上传文件查询": { i: 1, v: 4, d: "上传文件查询" },
    "上传文件删除": { i: 1, v: 8, d: "上传文件删除" },
    "库存创建": { i: 1, v: 16, d: "库存创建" },
    "库存修改": { i: 1, v: 32, d: "库存修改" },
    "库存查询": { i: 1, v: 64, d: "库存查询" },
    "库存删除": { i: 1, v: 128, d: "库存删除" },

    "库存变动日志创建": { i: 2, v: 1, d: "库存变动日志创建" },
    "库存变动日志修改": { i: 2, v: 2, d: "强制修改其他库存变动日志信息" },
    "库存变动日志查询": { i: 2, v: 4, d: "库存变动日志查询" },
    "库存变动日志删除": { i: 2, v: 8, d: "库存变动日志删除" },
    "商品创建": { i: 2, v: 16, d: "商品创建" },
    "商品修改": { i: 2, v: 32, d: "商品修改" },
    "商品查询": { i: 2, v: 64, d: "商品查询" },
    "商品删除": { i: 2, v: 128, d: "商品删除" },

    "订单创建": { i: 3, v: 1, d: "订单创建" },
    "订单修改": { i: 3, v: 2, d: "强制修改其他订单信息" },
    "订单查询": { i: 3, v: 4, d: "订单查询" },
    "订单删除": { i: 3, v: 8, d: "订单删除" },
    "消息创建": { i: 3, v: 16, d: "消息创建" },
    "消息修改": { i: 3, v: 32, d: "消息修改" },
    "消息查询": { i: 3, v: 64, d: "消息查询" },
    "消息删除": { i: 3, v: 128, d: "消息删除" },

    "用户分组创建": { i: 4, v: 1, d: "用户分组创建" },
    "用户分组修改": { i: 4, v: 2, d: "强制修改其他用户分组信息" },
    "用户分组查询": { i: 4, v: 4, d: "用户分组查询" },
    "用户分组删除": { i: 4, v: 8, d: "用户分组删除" },
    "用户创建": { i: 4, v: 16, d: "用户创建" },
    "用户修改": { i: 4, v: 32, d: "用户修改" },
    "用户查询": { i: 4, v: 64, d: "用户查询" },
    "用户删除": { i: 4, v: 128, d: "用户删除" },

    "系统设置创建": { i: 5, v: 1, d: "系统设置创建" },
    "系统设置修改": { i: 5, v: 2, d: "系统设置修改" },
    "系统设置查询": { i: 5, v: 4, d: "系统设置查询" },
    "系统设置删除": { i: 5, v: 8, d: "系统设置删除" },
    "商品类型创建": { i: 5, v: 16, d: "商品类型创建" },
    "商品类型修改": { i: 5, v: 32, d: "商品类型修改" },
    "商品类型查询": { i: 5, v: 64, d: "商品类型查询" },
    "商品类型删除": { i: 5, v: 128, d: "商品类型删除" },

    "入库单查询": { i: 6, v: 1, d: "入库单查询" },
    "入库单创建": { i: 6, v: 2, d: "入库单创建" },
    "入库单红冲": { i: 6, v: 4, d: "入库单红冲" },
    "出库单查询": { i: 6, v: 8, d: "出库单查询" },
    "出库单创建": { i: 6, v: 16, d: "出库单创建" },
    "出库单红冲": { i: 6, v: 32, d: "出库单红冲" },
    "调拨单查询": { i: 6, v: 64, d: "调拨单查询" },
    "调拨单创建": { i: 6, v: 128, d: "调拨单创建" },
    "调拨单红冲": { i: 7, v: 1, d: "调拨单红冲" },
    "消费单查询": { i: 7, v: 2, d: "消费单查询" },
    "查询所有单据": { i: 7, v: 4, d: "查询所有单据" },
    "保留2": { i: 7, v: 8, d: "保留2" },
    "奖品创建": { i: 7, v: 16, d: "奖品创建" },
    "奖品修改": { i: 7, v: 32, d: "奖品修改" },
    "奖品查询": { i: 7, v: 64, d: "奖品查询" },
    "奖品删除": { i: 7, v: 128, d: "奖品删除" },

  };
const exg = { gd, pd, format, getSetting, fUrl };

async function init() {
  // @ts-ignore
  if (!globalThis.unid) {
    for (const key in exg) {
      // @ts-ignore
      globalThis[key] = exg[key];
      // #ifdef MP-WEIXIN
      // @ts-ignore
      global[key] = exg[key];
      // #endif
    }
    Object.defineProperty(globalThis, "unid", { get() { return md.unid.NextId().toString(); } });
  }
}
init();
